package com.qianfeng.openapi.managerplateform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.managerplateform.dao.AppInfoDao;
import com.qianfeng.openapi.managerplateform.pojo.AppInfo;
import com.qianfeng.openapi.managerplateform.pojo.ResponseData;
import com.qianfeng.openapi.managerplateform.service.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoDao appInfoDao;
    ResponseData responseData = new ResponseData();
    @Override
    public ResponseData getAllAppInfo(int page, int limit) {
        PageHelper.startPage(page, limit);//开启分页-------放在第一行
        List<AppInfo> infoList = appInfoDao.getAllAppInfo();
        PageInfo pageInfo =new PageInfo(infoList);
        int count= (int) pageInfo.getTotal();
        ResponseData responseData = new ResponseData();
        if(infoList!=null){
        responseData.setCode(0);
        responseData.setMsg(ResponseData.SUCCESS_MSG);
        responseData.setData(infoList);
        responseData.setCount(count);
        return responseData;
        }
        responseData.setCode(1);
        responseData.setMsg(ResponseData.FAIL_MSG);
        return responseData;
    }

    @Override
    public boolean addAppInfo(AppInfo appInfo) {
        int i = appInfoDao.addAppInfo(appInfo);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public ResponseData delAppInfo(int[] ids) {
       for(int id:ids) {
           int i = appInfoDao.delAppInfo(id);
           if (i == 0) {
               responseData.setCode(0);
               responseData.setMsg("false");
               responseData.setData(i);
               return responseData;
           }

       }
        responseData.setCode(1);
        responseData.setMsg("true");
        return responseData;
    }

    @Override
    public boolean updateAllInfo(AppInfo appInfo) {
        int i = appInfoDao.updateAppInfo(appInfo);
        if(i>0){
            return true;
        }
        return false;
    }
}
